package com.foreveredu.propms.web.struts.action;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.Department;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.DepartmentActionForm;

public class DepartmentAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到部门管理列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);

		return mapping.findForward("deptList");
	}

	// 完成到部门添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DepartmentActionForm daf = (DepartmentActionForm) form;
		Department dept = new Department();
		PropertyUtils.copyProperties(dept, daf);
		// 判断当前输入的部门编号是否合法！
		if (daf.getDeptId() == null || "".equals(daf.getDeptId())) {

			request.setAttribute("dept", dept);
			request.setAttribute("deptIdError", "请输入部门编号！");
			return mapping.findForward("toDeptList");
		} else if (managementService.findDepartmentByDeptId(daf.getDeptId()) != null) {
			request.setAttribute("dept", dept);
			request.setAttribute("deptIdRequest", "该编号已存在，请重新输入！");

			return mapping.findForward("toDeptList");
		}
		if(!Pattern.matches("^[0-9]+$", daf.getDeptId())){
			request.setAttribute("dept", dept);
			request.setAttribute("deptIdError", "部门编号必须为数字组成！");
			return mapping.findForward("toDeptList");
		}

		if (daf.getDeptName() == null || "".equals(daf.getDeptName())) {

			request.setAttribute("dept", dept);
			request.setAttribute("deptNameError", "请输入部门名称！");

			return mapping.findForward("toDeptList");
		}

		managementService.saveDepartment(dept);

		return mapping.findForward("toDeptList");
	}

	// 跳转到部门修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DepartmentActionForm daf = (DepartmentActionForm) form;
		Department dept = managementService.findDepartmentById(daf.getId());
		request.setAttribute("dept", dept);

		return mapping.findForward("deptUpdate");
	}

	// 保存部门修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DepartmentActionForm daf = (DepartmentActionForm) form;

		// 判断当前输入的部门编号是否合法！
		if (!daf.getDeptId().equals(
				managementService.findDepartmentById(daf.getId()).getDeptId())) { // 判断提交的部门编号是否被用户修改

			if (daf.getDeptId() == null || "".equals(daf.getDeptId())) {

				Department dept = managementService.findDepartmentById(daf
						.getId());
				request.setAttribute("dept", dept);

				request.setAttribute("deptIdError", "请输入部门编号！");
				return mapping.findForward("deptUpdate");
			} else if (managementService
					.findDepartmentByDeptId(daf.getDeptId()) != null) {

				Department dept = managementService.findDepartmentById(daf
						.getId());
				request.setAttribute("dept", dept);

				request.setAttribute("deptIdRequest", "该编号已存在，请重新输入！");

				return mapping.findForward("deptUpdate");
			}
		}
		
		if(!Pattern.matches("^[0-9]+$", daf.getDeptId())){
			
			Department dept = managementService.findDepartmentById(daf.getId());
			request.setAttribute("dept", dept);
			request.setAttribute("deptIdError", "部门编号必须为数字组成！");
			return mapping.findForward("toDeptList");
		}
		if (daf.getDeptName() == null || "".equals(daf.getDeptName())) {
			Department dept = managementService.findDepartmentById(daf.getId());
			request.setAttribute("dept", dept);

			request.setAttribute("deptNameError", "请输入部门名称！");

			return mapping.findForward("deptUpdate");
		}

		Department dept = managementService.findDepartmentById(daf.getId());
		dept.setDeptId(daf.getDeptId());
		dept.setDeptName(daf.getDeptName());
		managementService.updateDepartment(dept);

		return mapping.findForward("toDeptList");
	}

	// 完成部门删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DepartmentActionForm daf = (DepartmentActionForm) form;
		managementService.deleteDepartmentById(daf.getId());

		return mapping.findForward("toDeptList");
	}
	
}
